package controller

import (
	"encoding/json"
	"github.com/gofiber/fiber"
	"gowork/bidding/model"
	"strconv"
)

type BiddingController struct {
	Bidding *model.Bidding
}

func (bc *BiddingController)Matching(c *fiber.Ctx){
	bc.Bidding = new(model.Bidding)

	bidingIdStr := c.FormValue("bidingId")
	bidingId, err := strconv.ParseInt(bidingIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := bc.Bidding.Matching(bidingId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (bc *BiddingController)Distribution(c *fiber.Ctx){
	bc.Bidding = new(model.Bidding)

	var bidding model.Bidding
	biddingStr := c.FormValue("bidding")
	errt := json.Unmarshal([]byte(biddingStr), &bidding)
	if errt != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": errt.Error(),
		})
		return
	}

	var supplierList []*model.Supplier
	supplierListStr := c.FormValue("supplierList")
	errs := json.Unmarshal([]byte(supplierListStr), &supplierList)
	if errs != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": errs.Error(),
		})
		return
	}
	res , err := bc.Bidding.Distribution(&bidding, supplierList)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (bc *BiddingController)Actions(c *fiber.Ctx){
	bc.Bidding = new(model.Bidding)

	tebderAllocationIdStr := c.FormValue("tebderAllocationId")
	tebderAllocationId, err := strconv.ParseInt(tebderAllocationIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := bc.Bidding.Actions(tebderAllocationId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (bc *BiddingController)Operations(c *fiber.Ctx){
	bc.Bidding = new(model.Bidding)

	tebderAllocationIdStr := c.FormValue("tebderAllocationId")
	tebderAllocationId, err := strconv.ParseInt(tebderAllocationIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	operation := c.FormValue("operation")

	remark := c.FormValue("remark")

	sampleNumber := c.FormValue("sampleNumber")

	res , err := bc.Bidding.Operations(tebderAllocationId, operation, remark, sampleNumber)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (bc *BiddingController)ScmActions(c *fiber.Ctx){
	bc.Bidding = new(model.Bidding)

	tebderAllocationIdStr := c.FormValue("tebderAllocationId")
	tebderAllocationId, err := strconv.ParseInt(tebderAllocationIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := bc.Bidding.ScmActions(tebderAllocationId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (bc *BiddingController)SampleEvaluation(c *fiber.Ctx){

	bc.Bidding = new(model.Bidding)

	tebderAllocationIdStr := c.FormValue("tebderAllocationId")
	tebderAllocationId, err := strconv.ParseInt(tebderAllocationIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	var evaluating model.Evaluating
	evaluatingStr := c.FormValue("evaluating")
	errs := json.Unmarshal([]byte(evaluatingStr), &evaluating)
	if errs != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": errs.Error(),
		})
		return
	}
	res , err := bc.Bidding.SampleEvaluation(tebderAllocationId, &evaluating)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}























